package command;

import parser.ChessContentHandlerConstants;
import parser.ParserConstants;
import chess.Player;
import chess.Tile;

public class EnPassantAttackCommand extends AttackCommand {

	private Tile killedPawnTile;
	public EnPassantAttackCommand(Tile orig, Tile dest, Player curPlayer) {
		super(orig, dest, curPlayer);
		if (curPlayer.getColour().equals("White")){
			killedPawnTile=orig.getBoard().getTile(dest.getX(), dest.getY()+1);
		}
		else if (curPlayer.getColour().equals("Black")){
			killedPawnTile=orig.getBoard().getTile(dest.getX(), dest.getY()-1);
		}
		pieceKilled=killedPawnTile.getPieceHere();
	}

	@Override
	/*
	 * Undoes the attack.
	 * This needed to be overridden from the default attack Undo, because 
	 * the killed piece doesn't go where the killing piece moved to.
	 * <pre>
	 * Max Joyce
	 * -Created (Feb 21, 2010)
	 * </pre>
	 */
	public void undo() {
		secondaryCommand.undo();
		this.destTile.getPieceHere().move(origTile);
		this.killingPlayer.getKills().remove(pieceKilled);
		killedPawnTile.setPieceHere(pieceKilled);
		pieceKilled.setCurrentTile(killedPawnTile);
		this.origTile.getPieceHere().setMoved(movedPreviously);
	}
	/**
	 * This method will return a string representation of the command used for importing and
	 * exporting
	 * @return Returns a string representing the object
	 */
	public String exportCode(){
		return "EnPassant:"+origTile.toString()+"to"+destTile.toString() + "with" + this.secondaryCommand.exportCode();
	}
	/**
	 * This method will return a string representation of the command used for saving and
	 * loading
	 * @return Returns a string representing the object
	 */
	@Override
	public String xmlCode(){
		String code = "<" + ParserConstants.ELEMENT_ENPASSANT + ">\n";
		
		code += "\t" + origTile.XMLPointTag(ChessContentHandlerConstants.POINT_1).replaceAll("\n", "\n\t");
		code += "\n";
		code += "\t" + destTile.XMLPointTag(ChessContentHandlerConstants.POINT_2).replaceAll("\n", "\n\t");
		code += "\n";
		code += "</" + ParserConstants.ELEMENT_ENPASSANT + ">\n";
		code += secondaryCommand.xmlCode();
		return code;
	}
	@Override
	public Command getSecondaryCommand() {
		return secondaryCommand;
	}
}
